


library(mvtnorm)
library(arm)
library(BRugs)
library(R2OpenBUGS)
library(coda)
library(car)
library(foreign)

# Change this to your working data directory.  Note this path makes Dropbox work at least on a windows machine
setwd("~/../Dropbox/UCR/research/bayes/funglee/persuasion/OBOE/Replication/ResultsInPaper/Tutorial")


dataset<-read.dta(file.choose()) # read in a Stata file "tutorial_data.dta"




# globally set constants and variables
n_respondents <- length(dataset$groupid)  # number of units in the analysis
n_responses <- 5  # number of responses for each question.  For this example, we have 5 categories (ordinal/Likert)
n_sites <- 3  # number of "sites" exogenous variables  
postO1<-dataset$postO1  # this is the ordinal version of the DV
sites<-as.matrix(cbind(dataset$site2, dataset$site3))  # collecting the exogenous varaiables in a matrix objec



#  The data and inits objects are used below to create the data and initial value files to send to OpenBUGS
data<-list("postO1", "n_respondents", "n_responses", "n_sites", "sites") # to set up the data file, creates a list of the variable and constant names
inits <- function() {  # this is a function that generates random initial values
  list(beta.0=c(-1,-0.5,0.5,1), beta.3=rnorm(n_sites-1))} 


# Output the data and inits files to the working directory, using the data and inits objects defined above
setwd("~/../Dropbox/UCR/research/bayes/funglee/persuasion/OBOE/Replication/ResultsInPaper/Tutorial/basic_examples/ordinal")
bugs.data(data, dir=getwd(), digits=5, data.file="data.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits1.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits2.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits3.txt")

